﻿using Xiami.Helper.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using AMing.Extension.Net40;

namespace Xiami.Helper.Method
{
    public class LoadXmlData
    {
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public static Model.TrackListInfo LoadData(XmlDocument xmlDoc)
        {
            Model.TrackListInfo list = null;
            try
            {
                list = new TrackListInfo();
                list.TrackList = new List<TrackInfo>();
                XmlNode playlistNode = xmlDoc.ChildNodes[1];
                XmlNode trackListNode = playlistNode.ChildNodes[0];
                XmlNodeList trackLists = trackListNode.ChildNodes;
                foreach (XmlNode xmlnode in trackLists)
                {
                    list.TrackList.Add(LoadTrackInfo(xmlnode));
                }
                list.Uid = ReadXmlString(playlistNode, "uid");
                list.Type = ReadXmlString(playlistNode, "type");
                list.Type_id = ReadXmlString(playlistNode, "type_id");
                list.Clearlist = ReadXmlString(playlistNode, "clearlist");

                return list;
            }
            catch
            {
                return list;
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="trackXml"></param>
        private static Model.TrackInfo LoadTrackInfo(XmlNode trackXml)
        {
            Model.TrackInfo item = new Model.TrackInfo();
            item.Title = ReadXmlString(trackXml, "title");
            item.Song_id = ReadXmlString(trackXml, "song_id");
            item.Album_id = ReadXmlString(trackXml, "album_id");
            item.Album_name = ReadXmlString(trackXml, "album_name");
            item.object_id = ReadXmlString(trackXml, "object_id");
            item.object_name = ReadXmlString(trackXml, "object_name");
            item.insert_type = ReadXmlString(trackXml, "insert_type");
            item.background = ReadXmlString(trackXml, "background");
            item.grade = ReadXmlString(trackXml, "grade");
            item.artist = ReadXmlString(trackXml, "artist");
            item.Location = ReadXmlString(trackXml, "location");
            item.ms = ReadXmlString(trackXml, "ms");
            item.Lyric = ReadXmlString(trackXml, "lyric");
            item.Pic = ReadXmlString(trackXml, "pic");
            item.Length = ReadXmlString(trackXml, "length").To<int>(0);
            item.MP3Url = DecodeUrl.DecodeDownloadPath(item.Location);

            return item;
        }

        private static string ReadXmlString(XmlNode node, string columnName)
        {
            try
            {
                return node[columnName].InnerText;
            }
            catch
            {
                return null;
            }
        }
    }
}
